package com.example.restful.controller;

import com.example.restful.common.CommonResponse;
import com.example.restful.common.LinkDTO;
import com.example.restful.common.LinkInfo;
import com.example.restful.domain.Borrow;
import com.example.restful.service.BorrowService;
import com.example.restful.service.MaterialService;
import com.example.restful.service.PersonnelService;
import com.example.restful.utils.UrlUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("borrows")
@Tag(name = "实验室物资借用、归还记录")
public class BorrowController {
    @Autowired
    BorrowService borrowService;
    @Autowired
    PersonnelService personnelService;
    @Autowired
    MaterialService materialService;


    @GetMapping()
    @Operation(summary = "查询所有借用记录")
    public CommonResponse<Object> getAll() {
        List<Borrow> borrowList = borrowService.selectAll();
        if (borrowList.isEmpty()) {
            return CommonResponse.createForSuccess("无借用记录");
        }
        List<LinkDTO<Borrow>> res = new ArrayList<>();
        for (Borrow borrow : borrowList) {
            Map<String,String> urls = new HashMap<>();
            urls.put(LinkInfo.borrowInfo,UrlUtil.getUrlWithArg(BorrowController.class, "getById", borrow.getId()));
            urls.put(LinkInfo.personnelInfo,UrlUtil.getUrlWithArg(PersonnelController.class, "getById", borrow.getPersonnelId()));
            urls.put(LinkInfo.materialInfo,UrlUtil.getUrlWithArg(MaterialController.class, "getById", borrow.getMaterialId()));
            LinkDTO<Borrow> linkDTO = new LinkDTO<>(borrow, urls);
            res.add(linkDTO);
        }
        return CommonResponse.createForSuccess("查询借用记录成功", res);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取某个指定借用记录的信息")
    public CommonResponse<Object> getById(@PathVariable int id) {
        Borrow borrow = borrowService.selectById(id);
        List<LinkDTO<Borrow>> res = new ArrayList<>();

        if (borrow == null) return CommonResponse.createForSuccess("未找到相关借用记录", res);

        Map<String,String> urls = new HashMap<>();
        urls.put(LinkInfo.allBorrows,UrlUtil.getUrl(BorrowController.class, "getAll"));
        urls.put(LinkInfo.personnelInfo,UrlUtil.getUrlWithArg(PersonnelController.class, "getById", borrow.getPersonnelId()));
        urls.put(LinkInfo.materialInfo,UrlUtil.getUrlWithArg(MaterialController.class, "getById", borrow.getMaterialId()));
        LinkDTO<Borrow> linkDTO = new LinkDTO<>(borrow, urls);
        res.add(linkDTO);
        return CommonResponse.createForSuccess("查询借用记录成功", res);
    }

    @PostMapping()
    @Operation(summary = "添加一条借用记录")
    public CommonResponse<Object> addBorrow(@RequestBody Borrow borrow) {

        if (borrowService.addItem(borrow)) {
            return CommonResponse.createForSuccess("添加借用记录成功", borrow);
        }
        return CommonResponse.createForError("添加借用记录失败");
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新某个指定借用记录的信息")
    public CommonResponse<Object> update(@PathVariable int id, @RequestBody Borrow borrow) {
        if (borrowService.updateById(id, borrow)) {
            borrow.setId(id);
            Borrow newBorrow = borrowService.selectById(id);
            return CommonResponse.createForSuccess("修改借用记录成功", newBorrow);
        }
        return CommonResponse.createForError("修改借用记录失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除某条借用记录")
    public CommonResponse<Object> delete(@PathVariable int id) {
        if (borrowService.deleteById(id)) {
            return CommonResponse.createForSuccess("删除成功");
        }
        return CommonResponse.createForError("删除失败");
    }

    @GetMapping("/searchMaterialId")
    @Operation(summary = "根据借用材料获取某个指定借用记录的信息")
    public CommonResponse<Object> getByMaterialId(@RequestParam("materialId") int materialId) {

        List<Borrow> borrowList = borrowService.selectByMaterialId(materialId);
        if (borrowList.isEmpty()) {
            return CommonResponse.createForSuccess("无借用记录");
        }
        List<LinkDTO<Borrow>> res = new ArrayList<>();
        for (Borrow borrow : borrowList) {
            Map<String,String> urls = new HashMap<>();
            urls.put(LinkInfo.borrowInfo,UrlUtil.getUrlWithArg(BorrowController.class, "getById", borrow.getId()));
            urls.put(LinkInfo.personnelInfo,UrlUtil.getUrlWithArg(PersonnelController.class, "getById", borrow.getPersonnelId()));
            urls.put(LinkInfo.materialInfo,UrlUtil.getUrlWithArg(MaterialController.class, "getById", borrow.getMaterialId()));
            LinkDTO<Borrow> linkDTO = new LinkDTO<>(borrow, urls);
            res.add(linkDTO);
        }
        return CommonResponse.createForSuccess("查询借用记录成功", res);
    }

    @GetMapping("/searchPersonnelId")
    @Operation(summary = "根据实验室人员获取某个指定借用记录的信息")
    public CommonResponse<Object> getByPersonnelId(@RequestParam("personnelId") int personnelId) {
        List<Borrow> borrowList = borrowService.selectByPersonnelId(personnelId);
        if (borrowList.isEmpty()) {
            return CommonResponse.createForSuccess("无借用记录");
        }
        List<LinkDTO<Borrow>> res = new ArrayList<>();
        for (Borrow borrow : borrowList) {
            Map<String,String> urls = new HashMap<>();
            urls.put(LinkInfo.borrowInfo,UrlUtil.getUrlWithArg(BorrowController.class, "getById", borrow.getId()));
            urls.put(LinkInfo.personnelInfo,UrlUtil.getUrlWithArg(PersonnelController.class, "getById", borrow.getPersonnelId()));
            urls.put(LinkInfo.materialInfo,UrlUtil.getUrlWithArg(MaterialController.class, "getById", borrow.getMaterialId()));
            LinkDTO<Borrow> linkDTO = new LinkDTO<>(borrow, urls);
            res.add(linkDTO);
        }
        return CommonResponse.createForSuccess("查询借用记录成功", res);
    }
}
